package com.wenlin.myproduction.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.wenlin.myproduction.R;
import com.wenlin.myproduction.util.Checker;
import com.wenlin.myproduction.util.UI;

public class PicProgressView extends View {
    private int viewWidth = (int) UI.getPx(R.dimen.wpx290);
    private int viewHeight = viewWidth;
    private Paint paint;
    private Rect rectShow;
    private boolean sad;//默认是笑脸
    private Bitmap[] arrBitmap = new Bitmap[2];
    private static final int[] ARR_SMILE_PIC_IDS = {R.mipmap.me_ic_smile_gray, R.mipmap.me_ic_smile_active};
    private static final int[] ARR_SAD_PIC_IDS = {R.mipmap.me_ic_sad_gray, R.mipmap.me_ic_sad_active};
    private static final int STATE_ACTIVE = 1;
    private static final int STATE_DEFAULT = 0;
    private int progress;
    private float picWidth = UI.getPx(R.dimen.wpx290);
    private float picHeight = UI.getPx(R.dimen.wpx290);

    public PicProgressView(Context context) {
        this(context, null);
    }

    public PicProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PicProgressView);
        sad = ta.getBoolean(R.styleable.PicProgressView_ppv_sad, false);
        ta.recycle();
        initialize();
    }

    private void initialize() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);

        rectShow = new Rect();
    }

    /**
     * 设置进度
     *
     * @param progress 进度
     */
    public void setProgress(int progress) {
        this.progress = progress;
        if (this.progress > 100) this.progress = 100;
        if (this.progress < 0) this.progress = 0;
        invalidate();
    }

    /**
     * 获取激活或者默认状态对应的图片资源编号
     *
     * @param active 是否是激活状态
     * @return 所求
     */
    private int getResId(int active) {
        int[] pics = sad ? ARR_SAD_PIC_IDS : ARR_SMILE_PIC_IDS;
        return pics[active];
    }

    /**
     * 从缓存中获取Bitmap图片
     *
     * @param active 是否是激活状态
     * @return 所求
     */
    private Bitmap getBitmapFromCache(int active) {
        Bitmap bitmap = arrBitmap[active];
        if (Checker.isNull(bitmap)) {
            bitmap = BitmapFactory.decodeResource(getResources(), getResId(active));
            bitmap = handleBitmap(bitmap, picWidth, picHeight);
            arrBitmap[active] = bitmap;
        }
        return bitmap;
    }

    /**
     * 生成新的Bitmap图片
     *
     * @return 所求
     */
    private Bitmap handleBitmap(Bitmap bitmap, float newWidth, float newHeight) {
        Matrix matrix = new Matrix();//创建一个处理图片的类
        int width = bitmap.getWidth();//获取图片本身的大小(宽)
        int height = bitmap.getHeight();//获取图片本身的大小(高)
        float wS = newWidth / width;//缩放比---->这块注意这个是新的宽度/高度除以旧的宽度
        float hS = newHeight / height;//缩放比---->这块注意这个是新的宽度/高度除以旧的宽度
        matrix.postScale(wS, hS);//这块就是处理缩放的比例
        //matrix.setScale(sX,sY);//缩放图片的质量sX表示宽0.5就代表缩放一半,sX同样
        return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(getBitmapFromCache(STATE_DEFAULT), 0, 0, paint);
        int top = (int) (viewHeight * (100 - progress) / 100f);
        rectShow.set(0, top, viewWidth, viewHeight);
        canvas.drawBitmap(getBitmapFromCache(STATE_ACTIVE), rectShow, rectShow, paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measure(widthMeasureSpec, true), measure(heightMeasureSpec, false));
    }

    /**
     * 获取宽或者高
     *
     * @param measureSpec 测量单位
     * @param isWidSpec   是宽还是高
     * @return 所求
     */
    private int measure(int measureSpec, boolean isWidSpec) {
        int result;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            result = isWidSpec ? viewWidth : viewHeight;
            if (mode == MeasureSpec.AT_MOST) result = Math.min(result, size);
        }
        return result;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (Checker.notNull(arrBitmap) && arrBitmap.length > 0) {
            for (Bitmap bitmap : arrBitmap) {
                if (Checker.notNull(bitmap) && !bitmap.isRecycled())
                    bitmap.recycle();
            }
        }
    }
}
